﻿using DataType.Actions;
using DataType.JudgeResults;
using OprsSimulator.Operators;
using System.Collections.Generic;
using MapCalclator;
using System;
using DataType.CustomEnumType;
using OprsSimulator.OprAttributes;
using System.Diagnostics;
using System.Linq;

namespace ActionsJudge
{
    internal static class ChangeStateJudge
    {
        public static List<IJudgeResult> ToJudge(IBasicAction acts, List<IOperator> oprs, Map map)
        {
            List<IJudgeResult> judgeResults = new List<IJudgeResult>();

            ChangeStateAction action = (ChangeStateAction)acts;
            // 根据动作id找到算子变量
            IOperator opr = null;
            foreach (IOperator oper in oprs)
            {
                if (oper.ObjID == action.ObjID)// 如果id一致
                {
                    opr = oper;
                    break;
                }
            }
            if (opr == null)// 如果没找到算子
            {
                Trace.TraceWarning($"无效裁决：态势中没有ID为{action.ObjID}的算子");
                return new List<IJudgeResult>();
            }
            if (action.Type != ActionType.ChangeState)
            {
                Trace.TraceWarning("无效裁决：算子的动作类型有误，应该是改变状态类型");  
                return new List<IJudgeResult>();
            }
            // 没有实现接口
            if (!typeof(ICommonAttris).IsAssignableFrom(opr.GetType()) ||
                !typeof(IMoveAttris).IsAssignableFrom(opr.GetType()) ||
                !typeof(IAttackAttris).IsAssignableFrom(opr.GetType()) ||
                !typeof(IStateAttris).IsAssignableFrom(opr.GetType()))
            {
                Trace.TraceWarning($"无效裁决：算子{action.ObjID}不具有改变状态所需要的全部属性");
                return new List<IJudgeResult>();
            }

            //获取属性
            MoveStateType move_state = (MoveStateType)opr.GetType().GetProperty("MoveState").GetValue(opr);
            int change_state_remain_time = (int)opr.GetType().GetProperty("ChangeStateRemainTime").GetValue(opr);
            int speed = (int)opr.GetType().GetProperty("Speed").GetValue(opr);
            bool stop = (bool)opr.GetType().GetProperty("Stop").GetValue(opr);
            int weapon_unfold_state = (int)opr.GetType().GetProperty("WeaponUnfoldState").GetValue(opr);
            int weapon_unfold_time = (int)opr.GetType().GetProperty("WeaponUnfoldTime").GetValue(opr);
            int cur_hex = (int)opr.GetType().GetProperty("CurHex").GetValue(opr);
            bool keep = (bool)opr.GetType().GetProperty("Keep").GetValue(opr);
            OprType type = (OprType)opr.GetType().GetProperty("Type").GetValue(opr);
            int get_on_remain_time = (int)opr.GetType().GetProperty("GetOnRemainTime").GetValue(opr);
            int get_off_remain_time = (int)opr.GetType().GetProperty("GetOffRemainTime").GetValue(opr);
            int oprTire = (int)opr.GetType().GetProperty("Tire").GetValue(opr);

            if (get_on_remain_time > 0 || get_off_remain_time > 0)
            {
                Trace.TraceWarning($"无效裁决：算子正在上车或者下车,不能改变状态");
                return new List<IJudgeResult>();
            }
            if (change_state_remain_time > 0)
            {
                Trace.TraceWarning($"无效裁决：算子正在改变状态,不能执行改变状态命令");
                return new List<IJudgeResult>();
            }

            bool change_state_flag = false;// 可以转换状态的标志位
            // 1.判断能否转正常机动状态
            if (action.TargetState == MoveStateType.Normal)// 目标状态为机动
            {
                // 其他机动状态
                if (move_state == MoveStateType.Shelt || move_state == MoveStateType.March || move_state == MoveStateType.RushLevel1 || move_state == MoveStateType.RushLevel2 || move_state == MoveStateType.HalfSpeed)
                {
                    if (stop | speed <= 0.0)  // 只要停下来就能切换成正常机动状态
                    {
                        change_state_flag = true;
                    }
                }              
            }
            // 2.判断能否转行军状态
            else if (action.TargetState == MoveStateType.March && type == OprType.Vehicle)// 目标状态是行军，且是车辆算子（只有车辆可以行军）
            {
                // 在正常机动或者半速，且没有在转换状态
                if ((move_state == MoveStateType.Normal || move_state == MoveStateType.HalfSpeed) && change_state_remain_time <= 0)  // 判断是否处于正常状态
                {
                    if (weapon_unfold_state == 0 && weapon_unfold_time <= 0)  // 判断是否武器处于锁定状态
                    {
                        if (map[cur_hex / 100, cur_hex % 100].roads.Max() > 0)  // 判断是否在道路上
                        {
                            if (stop | speed <= 0.0)  // 必须已经停下来了
                            {
                                change_state_flag = true;
                            }
                        }
                    }
                }
            }
            // 3.判断能否转掩蔽状态
            else if (action.TargetState == MoveStateType.Shelt)
            {
                if (!keep) // 不能处于压制状态
                {
                    if (move_state == (int)MoveStateType.Normal || move_state == MoveStateType.HalfSpeed || move_state == MoveStateType.RushLevel1 || move_state == MoveStateType.RushLevel2)  // 判断是否处于正常状态
                    {
                        if (stop | speed <= 0.0)  // 必须已经停下来了
                        {
                            change_state_flag = true;
                        }
                    }
                }
            }
            // 3.判断能否转一级冲锋
            else if (action.TargetState == MoveStateType.RushLevel1)
            {
                if (!keep) // 不能处于压制状态
                {
                    if (move_state == (int)MoveStateType.Normal || move_state == MoveStateType.RushLevel2 || move_state == MoveStateType.Shelt)
                    {
                        if (stop | speed <= 0.0)  // 必须已经停下来了
                        {
                            change_state_flag = true;
                        }
                    }
                }
            }
            // 4.判断能否转二级冲锋
            else if (action.TargetState == MoveStateType.RushLevel2)
            {
                if (!keep) // 不能处于压制状态
                {
                    if (oprTire < 1)// 一级疲劳不能转换为二级冲锋状态
                    {
                        if (move_state == (int)MoveStateType.Normal || move_state == MoveStateType.RushLevel1 || move_state == MoveStateType.Shelt)
                        {
                            if (stop | speed <= 0.0)  // 必须已经停下来了
                            {
                                change_state_flag = true;
                            }
                        }
                    }
                }
            }
            // 5.判断能否转半速状态
            if (action.TargetState == MoveStateType.HalfSpeed && type == OprType.Vehicle)//人员不可半速
            {
                if (move_state == MoveStateType.Shelt || move_state == MoveStateType.Normal)
                {
                    if (stop | speed <= 0.0)  // 只要停下来就能切换成正常机动状态
                    {
                        change_state_flag = true;
                    }
                }
            }

            ChangeStateJudgeResult changeStateJudgeResult = new ChangeStateJudgeResult(action.ObjID, change_state_flag, action.TargetState);
            judgeResults.Add(changeStateJudgeResult);

            return judgeResults;
        }
    }
}
